package com.kirayous.gulimall.ware.service.impl;

import com.kirayous.common.constant.WareConstant;
import com.kirayous.gulimall.ware.entity.PurchaseDetailEntity;
import com.kirayous.gulimall.ware.vo.MergeVo;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kirayous.common.utils.PageUtils;
import com.kirayous.common.utils.Query;

import com.kirayous.gulimall.ware.dao.PurchaseDao;
import com.kirayous.gulimall.ware.entity.PurchaseEntity;
import com.kirayous.gulimall.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceivePurchase(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().eq("status",0).or().eq("status",1)
        );

        return new PageUtils(page);
    }

    @Override
    public void mergePurchase(MergeVo mergeVo) {

    }

//    @Transactional
//    @Override
//    public void mergePurchase(MergeVo mergeVo) {
//
//        //判断purchaseId的有无，来选择是否新建采购单
//        Long purchaseId = mergeVo.getPurchaseId();
//        if(purchaseId == null){
//            //1、新建一个
//            PurchaseEntity purchaseEntity = new PurchaseEntity();
//
//            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
//            purchaseEntity.setCreateTime(new Date());
//            purchaseEntity.setUpdateTime(new Date());
//            this.save(purchaseEntity);
//            purchaseId = purchaseEntity.getId();
//        }
//
//        //TODO 确认采购单状态是0,1才可以合并
//
//        List<Long> items = mergeVo.getItems();
//        Long finalPurchaseId = purchaseId;
//        List<PurchaseDetailEntity> collect = items.stream().map(i -> {
//            PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
//
//            detailEntity.setId(i);
//            detailEntity.setPurchaseId(finalPurchaseId);
//            detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
//            return detailEntity;
//        }).collect(Collectors.toList());
//
//
//        detailService.updateBatchById(collect);
//
//        PurchaseEntity purchaseEntity = new PurchaseEntity();
//        purchaseEntity.setId(purchaseId);
//        purchaseEntity.setUpdateTime(new Date());
//        this.updateById(purchaseEntity);
//    }

}